package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.cderp.domain.MaterialStock;
import com.ruoyi.cderp.mapper.MaterialStockMapper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Random;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.cderp.domain.ProductionReturnItem;
import com.ruoyi.cderp.mapper.ProductionReturnMapper;
import com.ruoyi.cderp.domain.ProductionReturn;
import com.ruoyi.cderp.service.IProductionReturnService;

/**
 * 生产退料Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-15
 */
@Service
public class ProductionReturnServiceImpl implements IProductionReturnService
{

    //引入库存Mapper
    @Autowired
    private MaterialStockMapper materialStockMapper;

    //引入生产退料Mapper
    @Autowired
    private ProductionReturnMapper productionReturnMapper;


    @Override
    public List<ProductionReturnItem> selectOrderReturnList(String id) {
        return productionReturnMapper.selectOrderReturnList(id);
    }


    /**
     * 查询生产退料
     *
     * @param id 生产退料主键
     * @return 生产退料
     */
    @Override
    public ProductionReturn selectProductionReturnById(String id)
    {
        ProductionReturn productionReturn = productionReturnMapper.selectProductionReturnById(id);
        productionReturn.setProductionReturnItemList(productionReturnMapper.selectProductionReturnItemList(id));
        return productionReturn;
    }

    /**
     * 查询生产退料列表
     *
     * @param productionReturn 生产退料
     * @return 生产退料
     */
    @Override
    public List<ProductionReturn> selectProductionReturnList(ProductionReturn productionReturn)
    {
        return productionReturnMapper.selectProductionReturnList(productionReturn);
    }

    /**
     * 新增生产退料
     *
     * @param productionReturn 生产退料
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductionReturn(ProductionReturn productionReturn)
    {
        productionReturn.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //创建人名称
        productionReturn.setCreateBy(loginUser.getUser().getUserName());
        int rows = productionReturnMapper.insertProductionReturn(productionReturn);
        insertProductionReturnItem(productionReturn);
        return rows;
    }

    /**
     * 修改生产退料
     *
     * @param productionReturn 生产退料
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProductionReturn(ProductionReturn productionReturn)
    {
        productionReturn.setUpdateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //修改人名称
        productionReturn.setUpdateBy(loginUser.getUser().getUserName());

        productionReturnMapper.deleteProductionReturnItemByReturnId(productionReturn.getId());
        insertProductionReturnItem(productionReturn);
        return productionReturnMapper.updateProductionReturn(productionReturn);
    }

    /**
     * 批量删除生产退料
     *
     * @param ids 需要删除的生产退料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionReturnByIds(String[] ids)
    {
        productionReturnMapper.deleteProductionReturnItemByReturnIds(ids);
        return productionReturnMapper.deleteProductionReturnByIds(ids);
    }

    /**
     * 删除生产退料信息
     *
     * @param id 生产退料主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductionReturnById(String id)
    {
        productionReturnMapper.deleteProductionReturnItemByReturnId(id);
        return productionReturnMapper.deleteProductionReturnById(id);
    }



    /**
     * 新增生产退料明细信息
     *
     * @param productionReturn 生产退料对象
     */
    public void insertProductionReturnItem(ProductionReturn productionReturn)
    {
        List<ProductionReturnItem> productionReturnItemList = productionReturn.getProductionReturnItemList();
        String id = productionReturn.getId();
        if (StringUtils.isNotNull(productionReturnItemList))
        {
            List<ProductionReturnItem> list = new ArrayList<ProductionReturnItem>();
            for (ProductionReturnItem productionReturnItem : productionReturnItemList)
            {
                MaterialStock materialStock = new MaterialStock();
                materialStock.setWarehouseId(productionReturnItem.getWarehouseId());
                materialStock.setMaterialId(productionReturnItem.getMaterialId());
                materialStock.setStockQuantity(productionReturnItem.getActualQuantity());
                materialStockMapper.updateMaterialStockReturn(materialStock);


                productionReturnItem.setReturnId(id);


                //随机生成组合商品编号
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String formattedDate = sdf.format(new Date());
                Random random = new Random();
                int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
                productionReturnItem.setId("167" + formattedDate+randomNumber);


                list.add(productionReturnItem);
            }
            if (list.size() > 0)
            {
                productionReturnMapper.batchProductionReturnItem(list);
            }
        }
    }
}
